package com.atdfrz.secondshop.controller.admin;

import com.atdfrz.secondshop.controller.user.BaseController;
import com.atdfrz.secondshop.dto.LayuiResult;
import com.atdfrz.secondshop.mapper.CategoryMapper;
import com.atdfrz.secondshop.mapper.ProductMapper;
import com.atdfrz.secondshop.pojo.Category;
import com.atdfrz.secondshop.pojo.Order;
import com.atdfrz.secondshop.pojo.Product;
import com.atdfrz.secondshop.pojo.User;
import com.atdfrz.secondshop.service.ProductService;
import com.atdfrz.secondshop.service.ex.PasswordNotMatchException;
import com.atdfrz.secondshop.service.ex.UserNotFoundException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminProductController extends BaseController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @RequestMapping("/getproductlist")
    @ResponseBody//查询普通用户及管理员列表信息
    public Map<String, Object> getproductlist(Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Product::getCreateTime).orderByDesc(Product::getUpdateTime);
        List<Product> products = productMapper.selectList(wrapper);
        for (Product product : products) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = product.getCreateTime();
            String str = simpleDateFormat.format(date);
            product.setCreateTimeStr(str);
        }

        PageInfo<Product> pageInfo = new PageInfo<>(products);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("data", pageInfo.getList());
        //count是total
        map.put("count", pageInfo.getTotal());
        return map;
    }
    /**
     * 将商品下架或过审（status）
     * 0：正常  1：封号
     */
    @PutMapping("/changecommstatus/{productId}/{commstatus}")
    @ResponseBody
    public LayuiResult userCommstatus(@PathVariable("productId") Integer productId,@PathVariable("commstatus") Integer commstatus){
        LayuiResult<Product> layuiResult = new LayuiResult<>();
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>();
        wrapper.eq(productId!=null,Product::getProductId,productId);
        if (commstatus==0) {
            //解封处理
            Product product = productMapper.selectOne(wrapper);
            product.setState(2);
//            userMapper.insert(user);
            productMapper.update(product,wrapper);
            layuiResult.setCode(2);
            layuiResult.setMessage("下架成功");
            return layuiResult;
        }else if (commstatus==1){
            // 通过审核处理
            Product product = productMapper.selectOne(wrapper);
            product.setState(1);
            productMapper.update(product,wrapper);
            layuiResult.setCode(1);
            layuiResult.setMessage("审核通过");
        }
        return layuiResult;
    }
    
    @RequestMapping("/productlist")
    public String productlist(){
        return "/admin/product/productlist";
    }

    @RequestMapping("/getProductlistBycondition")
    @ResponseBody//根据商品的状态查询对应的商品列表
    public Map<String, Object> getOrderslistBycondition(Integer page, Integer limit,Integer state){
        PageHelper.startPage(page, limit);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Product::getCreateTime).orderByDesc(Product::getUpdateTime);
        wrapper.eq(state!=null,Product::getState,state);
        List<Product> productsList = productMapper.selectList(wrapper);
        for (Product product : productsList) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = product.getCreateTime();
            String str = simpleDateFormat.format(date);
            product.setCreateTimeStr(str);
        }
        PageInfo<Product> pageInfo = new PageInfo<>(productsList);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("data", pageInfo.getList());
        //count是total
        map.put("count", pageInfo.getTotal());
        return map;
    }

    }
//}
